package com.mifengs.mall.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;

import com.mifengs.mall.entity.ContactBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lynn on 2018/5/11.
 */

public class Contacts {

    public static String[] getContacts(Uri uri, Context context){
        String[] contact=new String[2];
        ContentResolver cr = context.getContentResolver();
        //取得联系人中第一项的光标
        Cursor cursor=cr.query(uri,null,null,null,null);
        if(cursor!=null)
        {
            cursor.moveToFirst();
            //取得联系人姓名
            int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            contact[0]=cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            if(phone != null){
                phone.moveToFirst();
                contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phone.close();
            cursor.close();
        }
        else
        {
            return null;
        }
        return contact;
    }

    public static List<ContactBean> getPhoneContacts(Context context) {
        List<ContactBean> contactsList =  new ArrayList<ContactBean>();
        ContentResolver resolver = context.getContentResolver();
        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
        // 获取手机联系人
        Cursor phoneCursor = resolver.query(uri,null,null,null,
               null);
        if (phoneCursor != null) {
            while (phoneCursor.moveToNext()) {
                ContactBean contactBean = new ContactBean();
                int contactId= phoneCursor.getInt(phoneCursor.getColumnIndex("_id"));
                String phoneName= phoneCursor.getString(phoneCursor.getColumnIndex("display_name"));
                contactBean.setContactsName(phoneName);
                //查询电话类型的数据操作
                Cursor phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                        null, null);
                while(phones.moveToNext())
                {
                    String phoneNumber = phones.getString(phones.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                    //添加Phone的信息
                    contactBean.setContactsPhone(phoneNumber);
                    contactsList.add(contactBean);
                }

            }
            for(int i = 0;i<contactsList.size();i++){
                Log.e("mfsc_"+i+"",contactsList.get(i).toString());
            }
            phoneCursor.close();
        }else{

        }
        return  contactsList;
    }
}
